package com.jinlejia.bbs.adapter.message;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.adapter.BaseLoadAdapter;
import com.jinlejia.bbs.bean.InviteBean;
import com.jinlejia.bbs.bean.base.TopicBean;
import com.jinlejia.bbs.databinding.BbsAdapterMessageInviteItemBinding;
import com.jinlejia.bbs.logic.message.MessageIgnoreInviteLogic;
import com.jinlejia.bbs.ui.reply.BBSReplyDetailActivity;

import java.util.ArrayList;

/**
 * @author 王进 on 2019/4/11.
 */
//消息 - 邀请回答列表
public class InviteListAdapter extends BaseLoadAdapter<ArrayList<InviteBean>> {

    public interface IgnoreListener{
        void onIgnoreSuccess();
    }
    private IgnoreListener listener;
    public InviteListAdapter(Context context,IgnoreListener listener) {
        super(context);
        this.listener = listener;
    }

    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case Inner.TYPE_ITEM:
                viewHolder = new ItemViewHolder(inflater.inflate(R.layout.bbs_adapter_message_invite_item, parent, false));
                break;
        }
        return viewHolder;
    }

    @Override
    protected int getItemViewTypeZ(int position) {
        return Inner.TYPE_ITEM;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            bindItemViewHolder((ItemViewHolder) holder, position);
        }
    }

    @Override
    public int getItemCount() {
        if (mData == null || mData.size() == 0) {
            return 1;
        }
        return mData.size();
    }
    //region item
    private class ItemViewHolder extends RecyclerView.ViewHolder {

        ItemViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }
    private MessageIgnoreInviteLogic logic;
    private void bindItemViewHolder(ItemViewHolder holder, final int position) {
        if (logic==null)
            logic = new MessageIgnoreInviteLogic(mContext);
        final BbsAdapterMessageInviteItemBinding binding = DataBindingUtil.getBinding(holder.itemView);
        final InviteBean bean = mData.get(position);
        binding.setBean(bean);
        View.OnClickListener toTopicListener =new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, BBSReplyDetailActivity.class);
                TopicBean topic = new TopicBean();
                topic.setTopicCode(bean.getTopic().getTopicCode());
                intent.putExtra("isInvite",true);
                intent.putExtra("topic",topic);
                mContext.startActivity(intent);
            }
        };

        binding.topicClickView.setOnClickListener(toTopicListener);
        binding.titleTextView.setOnClickListener(toTopicListener);
        if (bean.isIgnoreState()){
            binding.ignoreTextView.setText("已忽略");
            binding.ignoreTextView.setEnabled(false);
        }else {
            binding.ignoreTextView.setText("忽略");
        }
        binding.ignoreTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logic.execute(bean.getObjectId(), new MessageIgnoreInviteLogic.LogicCallBack() {
                    @Override
                    public void onResult(boolean result) {
                        if (!result)return;
                        if (listener!=null){
                            listener.onIgnoreSuccess();
                        }else {
                            notifyItemChanged(position);
                        }
                    }
                });
            }
        });


    }
    //endregion

    private static class Inner {
        final static int TYPE_ITEM = 1;
    }
}
